package com.sgb.knowledgeshare.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import com.bumptech.glide.Glide;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.weight.BaseVideoListAdapter;
import com.sgb.knowledgeshare.weight.ControllerView;
import com.sgb.knowledgeshare.weight.LikeView;
import com.swgk.core.base.model.entity.VideoBean;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

public class RecommendAdapter extends BaseVideoListAdapter<RecommendAdapter.MyHolder,VideoBean> {

    public RecommendAdapter(Context context) {
        super(context);
    }
    private VideoCallback callback;
    public void setCallback(VideoCallback callback) {
        this.callback = callback;
    }
    private boolean isShowUser = true;
    public void setShowUser(boolean showUser) {
        isShowUser = showUser;
    }

    private boolean isShowComment = true;
    public void setShowComment(boolean showcomment) {
        isShowComment = showcomment;
    }
    @NonNull
    @Override
    public RecommendAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_recommend_video2, viewGroup, false);
        return new MyHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, @SuppressLint("RecyclerView") final int position) {
        super.onBindViewHolder(myHolder, position);
        Glide.with(context).load(list.get(position).getCover()).into(myHolder.ivCover);
        myHolder.controller.setVideoData(list.get(position), context,isShowComment);
        callback.setControllerView(myHolder.controller,list.get(position));
        myHolder.likeView.setOnLikeListener(new LikeView.OnLikeListener() {
            @Override
            public void onLikeListener() {
                if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false)) {
                        if (list.get(position).isLiked()) {
                            myHolder.controller.like();//点赞的状态
                        } else {
                            if (callback != null) {
                                callback.VideoLike(list.get(position),myHolder.controller);//点赞
                            }
                        }
                    } else {
                        MToast.showToast(context, "请实名认证完成后再进行点赞！");
                    }
                } else {
                    MToast.showToast(context, "请先登录账户！");
                }
            }
        });
        if (isShowUser) {
            myHolder.controller.setVisibility(View.VISIBLE);
        } else {
            myHolder.controller.setVisibility(View.GONE);
        }
        myHolder.likeView.setOnPlayPauseListener(new LikeView.OnPlayPauseListener() {
            @Override
            public void onPlayOrPause() {
                callback.onPlayOrPause();
            }
        });
    }
   public final class  MyHolder extends BaseVideoListAdapter.BaseHolder {
       private final ImageView ivCover;
       private final ImageView ivPlays;
       private final FrameLayout videoLayout;
       private final ControllerView controller;
       private final LikeView likeView;

       MyHolder(@NonNull View itemView) {
           super(itemView);
           controller = itemView.findViewById(R.id.controllers);
           ivCover = itemView.findViewById(R.id.ivCover);
           videoLayout = itemView.findViewById(R.id.video_layout);
           ivPlays = itemView.findViewById(R.id.iv_plays);
           likeView = itemView.findViewById(R.id.likeviews);
       }

       @Override
       public ImageView getCoverView() {
           return ivCover;
       }

       @Override
       public ViewGroup getContainerView() {
           return videoLayout;
       }

       @Override
       public ImageView getPlayIcon(){
           return ivPlays;
       }

   }

    public interface VideoCallback {
        void VideoLike(VideoBean bean, ControllerView controller);
        void setControllerView(ControllerView controller, VideoBean videoBean);
        void onPlayOrPause();
    }


}